package com.sky.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.ByteArrayInputStream;

@Data
@AllArgsConstructor
@Slf4j
public class AliOssUtil {

    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;

    /**
     * 文件上传
     *
     * @param bytes      文件字节数组
     * @param objectName 文件名（包括路径）
     * @return 文件访问路径
     */
    public String upload(byte[] bytes, String objectName) {
        return upload(bytes, objectName, "");
    }

    /**
     * 文件上传
     *
     * @param bytes      文件字节数组
     * @param objectName 文件名
     * @param pathPrefix 路径前缀
     * @return 文件访问路径
     */
    public String upload(byte[] bytes, String objectName, String pathPrefix) {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 构建完整的 objectName
            String fullObjectName = pathPrefix.isEmpty() ? objectName : pathPrefix + "/" + objectName;

            // 创建PutObject请求。
            ossClient.putObject(bucketName, fullObjectName, new ByteArrayInputStream(bytes));
        } catch (OSSException oe) {
            log.error("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            log.error("Error Message: {}", oe.getErrorMessage());
            log.error("Error Code: {}", oe.getErrorCode());
            log.error("Request ID: {}", oe.getRequestId());
            log.error("Host ID: {}", oe.getHostId());
        } catch (ClientException ce) {
            log.error("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            log.error("Error Message: {}", ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        // 文件访问路径规则 https://BucketName.Endpoint/ObjectName
        StringBuilder stringBuilder = new StringBuilder("https://");
        stringBuilder
                .append(bucketName)
                .append(".")
                .append(endpoint)
                .append("/")
                .append(pathPrefix.isEmpty() ? objectName : pathPrefix + "/" + objectName);

        log.info("文件上传到: {}", stringBuilder.toString());

        return stringBuilder.toString();
    }
    //public String upload(byte[] bytes, String objectName) {
    //
    //    // 创建OSSClient实例。
    //    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    //
    //    try {
    //        // 创建PutObject请求。
    //        ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
    //    } catch (OSSException oe) {
    //        System.out.println("Caught an OSSException, which means your request made it to OSS, "
    //                + "but was rejected with an error response for some reason.");
    //        System.out.println("Error Message:" + oe.getErrorMessage());
    //        System.out.println("Error Code:" + oe.getErrorCode());
    //        System.out.println("Request ID:" + oe.getRequestId());
    //        System.out.println("Host ID:" + oe.getHostId());
    //    } catch (ClientException ce) {
    //        System.out.println("Caught an ClientException, which means the client encountered "
    //                + "a serious internal problem while trying to communicate with OSS, "
    //                + "such as not being able to access the network.");
    //        System.out.println("Error Message:" + ce.getMessage());
    //    } finally {
    //        if (ossClient != null) {
    //            ossClient.shutdown();
    //        }
    //    }
    //
    //    //文件访问路径规则 https://BucketName.Endpoint/ObjectName
    //    StringBuilder stringBuilder = new StringBuilder("https://");
    //    stringBuilder
    //            .append(bucketName)
    //            .append(".")
    //            .append(endpoint)
    //            .append("/")
    //            .append(objectName);
    //
    //    log.info("文件上传到:{}", stringBuilder.toString());
    //
    //    return stringBuilder.toString();
    //}
}
